package sg.edu.nus.iss.vmcs.customer;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.vmcs.store.DrinksBrand;
import sg.edu.nus.iss.vmcs.store.DrinksStore;
import sg.edu.nus.iss.vmcs.store.DrinksStoreItem;
import sg.edu.nus.iss.vmcs.store.Store;
import sg.edu.nus.iss.vmcs.store.StoreController;
import sg.edu.nus.iss.vmcs.store.StoreItem;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 * This interface object is part of the Customer Panel. It is used by the 
 * Customer to select a drink
 * 
 * @version 1.0 11 Oct 2008
 * @author Toh Kim Huat
 */

public class DrinkSelectionBox extends ProductSelectionBox {

	/**
	 * Generated serial version
	 */
	private static final long serialVersionUID = 7997028074160002088L;

	/**
	 * Array of integers providing identifiers for each selection button.
	 */
	private int[] drinkIdentifiers;
	
	/**
	 * Items
	 */
	private List<DrinkSelectionItem> items;
	
	/**
	 * Transaction controller
	 */
	private TransactionController transactionController;
	
	/**
	 * This constructor creates an instance of the object
	 * 
	 * @param customerPanel
	 *            The parent customer panel
	 * @param transactionController
	 *            The transaction controller controlling this object
	 */
	public DrinkSelectionBox(CustomerPanel customerPanel,
			TransactionController transactionController) {

		this.transactionController = transactionController;
		this.setLayout(new GridLayout(0, 1));
		this.items = new ArrayList<DrinkSelectionItem>();

		// get the drinks
		StoreController storeController = transactionController
				.getMainController().getStoreController();
		Store drinkStore = storeController.getStore(Store.PRODUCT);
		int storeSize = drinkStore.getStoreSize();
		ActionListener drinkSelectionListener = new DrinkSelectionListener(
				transactionController);
		this.drinkIdentifiers = new int[storeSize];
		for (int i = 0; i < storeSize; i++) {
			this.drinkIdentifiers[i] = i;
			DrinksStoreItem drinkStoreItem = (DrinksStoreItem) drinkStore
					.getStoreItem(i);
			DrinkSelectionItem currItem = new DrinkSelectionItem();
			currItem.setName(drinkStoreItem.getContent().getName());
			currItem.setPrice(((DrinksBrand) drinkStoreItem.getContent())
					.getPrice());
			currItem.setItemState(drinkStoreItem.getQuantity() > 0);
			currItem.addListener(drinkSelectionListener);
			currItem.getDrinkButton().setActionCommand(String.valueOf(i));
			this.add(currItem);
			this.items.add(currItem);
		}

	}
	
	/**
	 * This operation will activate or deactivate the drink selection buttons.
	 * 
	 * @param active
	 *            Boolean to indicate whether is active
	 */
	public void setActive(boolean active) {
		if (active) {
			// Retrieves the drink store
			DrinksStore drinksStore = (DrinksStore) this.transactionController
					.getMainController().getStoreController().getStore(
							Store.PRODUCT);

			// Updates the state
			for (int i = 0; i < drinksStore.getStoreSize(); i++) {
				setItemState(i, active);
			}
		} else {
			// Simply disables all drink selection items
			for (int i = 0; i < this.items.size(); i++) {
				this.items.get(i).setEnabled(false);
			}
		}
	}
	
	/**
	 * This operation activates or deactivates the drink selection buttons on
	 * the Customer Panel. This operation also displays OUT OF STOCK messages
	 * where appropriate.
	 * 
	 * @param brandIndex
	 *            Index of the brand
	 * @param active
	 *            Boolean to indicate whether item is active
	 */
	public void setItemState(int brandIndex, boolean active) {
		DrinksStore drinksStore = (DrinksStore) this.transactionController
				.getMainController().getStoreController().getStore(Store.PRODUCT);
		DrinksStoreItem drinkStoreItem = (DrinksStoreItem) drinksStore
				.getStoreItem(brandIndex);
		setState(brandIndex, active);
		this.items.get(brandIndex).setItemState(
				drinkStoreItem.getQuantity() > 0);
	}

	/**
	 * This operation activates or deactivates the drink selection buttons on
	 * the Customer Panel
	 * 
	 * @param brandIndex
	 *            The index of the brand
	 * @param active
	 *            Boolean to indicate whether button is active
	 */
	public void setState(int brandIndex, boolean active) {
		this.items.get(brandIndex).setEnabled(active);
	}
	
	
	/**
	 * Update a product brand based on the values received.
	 * 
	 * @param brand
	 *            The brand identifier
	 * @param storeItem
	 *            The store item
	 */
	public void update(int brand, StoreItem storeItem) {
		
		int quantity = storeItem.getQuantity();
		int price = ((DrinksBrand) storeItem.getContent()).getPrice();
		String name = ((DrinksBrand) storeItem.getContent()).getName();
		
		DrinkSelectionItem item = this.items.get(brand);
		item.setName(name);
		item.setPrice(price);
		System.out.println("name="+ name+ ";price="+ price +";quantity="+quantity+";(quantity > 0)=" + (quantity >0));
		item.setItemState(quantity > 0);
		
	}

	/**
	 * Get the drink identifiers
	 * 
	 * @return The drink identifiers
	 */
	public int[] getProductIdentifiers() {
		return drinkIdentifiers;
	}
	
}
